// pages/sqfb/sqfb.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cloudImgList: [],
        inputValue: '',
        userInfo:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getuserList()
    },

    getValue(e) {
        console.log(e.detail.value)
        this.setData({
            inputValue: e.detail.value
        })
    },
    //选择照片
    chooseImage() {
        let that = this
        wx.chooseImage({
            count: 9,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths
                console.log(tempFilePaths);
                that.setData({
                    cloudImgList: tempFilePaths
                })
            }
        })
    },
    //获取用户信息
    getuserList() {
        let that = this
        let user = wx.getStorageSync('user')
        let openid = user._openid
        wx.cloud.database().collection("user").where({ _openid: openid }).get({
            success(res) {
                that.setData({ userInfo: res.data })
            }
        })
    },
    //提交动态
    submitData(e) {
        wx.cloud.database().collection('dynamic').add({
            data: {
                nickName: this.data.userInfo[0].nickName,
                profile: this.data.userInfo[0].profile,
                text: this.data.inputValue,
                images: this.data.cloudImgList,
                time: Date.now(),
                commentList: [],
                dz: 0
            },
            success(res) {
                console.log(res)
                wx.navigateBack({
                    success(res) {
                        wx.showToast({
                            title: '发布成功!',
                        })
                    }
                })
            }
        })

    },
    //删除已上传照片
    deleteImg(e) {
        console.log(e.currentTarget.dataset.index)
        this.data.cloudImgList.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            cloudImgList: this.data.cloudImgList
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let user = wx.getStorageSync('user')
        console.log('缓存的user', user)
        this.setData({
            user: user,
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})